package com.example.social.screen

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.navigation.NavHostController
import com.example.social.R
import com.example.social.navigation.Routes
import com.example.social.utils.SharedPref
import kotlinx.coroutines.delay

//启动页
@Composable
fun Splash(
    navHostController: NavHostController
){
    val  context = LocalContext.current
    ConstraintLayout(modifier = Modifier.fillMaxSize()) {
        val (image) = createRefs() //创建一个可用于布局约束的引用
        Image(
            painter = painterResource(id = R.drawable.logo),
            contentDescription = "logo",
            modifier = Modifier
                .constrainAs(image) {  //布局约束
                    //在屏幕的水平和垂直方向上都居中
                    top.linkTo(parent.top)
                    bottom.linkTo(parent.bottom)
                    start.linkTo(parent.start)
                    end.linkTo(parent.end)
                }
                .size(120.dp)
        )
    }
    //参数 true 为立即执行 里面的代码块
    LaunchedEffect(true){
        delay(3000) //停留页面3s
        if (SharedPref.getUid(context).isNotEmpty()) { //获取当前用户信息,
            navHostController.navigate(Routes.BottomNav.routes){ //跳转到主页
                popUpTo(navHostController.graph.startDestinationId) //出栈
                launchSingleTop = true //复用现有实例
            }
        }else{
            navHostController.navigate(Routes.Login.routes) {//null 跳转到登录页面
                popUpTo(navHostController.graph.startDestinationId) //出栈
                launchSingleTop = true //复用现有实例
            }
        }
    }

}